﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Threading;
using ArgusLight.Fractals2D;
using ArgusLight.Fractals2D.ComplexFractalColoring;

namespace Fractals
{
	public partial class UIPeriodicColoringParameter : UIColoringParameter
	{
		const int MsPerPeriod = 8000;
		const int MsPerInterval = 100;
		DispatcherTimer timer;

		public UIPeriodicColoringParameter()
		{
			InitializeComponent();
			this.timer = new ArgusLight.Threading.Timer();
			this.timer.Interval = new System.TimeSpan(0, 0, 0, 0, MsPerInterval);
			this.timer.Tick += this.IncreaseTranslation;
		}

		void timer_Tick(object sender, EventArgs e)
		{
			//Dispatcher.BeginInvoke(this.IncreaseTranslation);
		}

		void IncreaseTranslation(object sender, EventArgs e)
		{
			double v = this.cTranslation.Value;
			//v += this.timer.Duration.TotalMilliseconds/MsPerPeriod;
			v += (double)MsPerInterval / MsPerPeriod;
			while (v > 1)
				v -= 1;

			this.cTranslation.Value = v;
		}

		private void cbAutoSetFrequency_Checked(object sender, RoutedEventArgs e)
		{
			this.cFrequency.IsEnabled = false;
			this.cNumberOfColorPeriods.IsEnabled = true;
		}

		private void cbAutoSetFrequency_Unchecked(object sender, RoutedEventArgs e)
		{
			this.cFrequency.IsEnabled = true;
			this.cNumberOfColorPeriods.IsEnabled = false;
		}

		private void cbAnimate_Checked(object sender, RoutedEventArgs e)
		{
			this.timer.Start();
		}

		private void cbAnimate_Unchecked(object sender, RoutedEventArgs e)
		{
			this.timer.Stop();
		}

		private void cTranslation_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
		{
			this.OnRedrawRequested();
		}

		private void cFrequency_ValueChanged(object sender, RoutedEventArgs e)
		{
			this.OnRedrawRequested();
		}

		private void cNumberOfColorPeriods_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
		{
			this.OnRedrawRequested();
		}

		public override void SetValues(ArgusLight.Fractals2D.ColoringParameter cParameter)
		{
			PeriodicColoringParameter para = (PeriodicColoringParameter)cParameter;
			this.cbAutoSetFrequency.IsChecked = para.AutoSetFrequency;
			this.cTranslation.Value = para.Translation;
			this.cFrequency.Value = para.Frequency;
			this.cNumberOfColorPeriods.Value = para.NumberOfColorPeriods;
		}

		public override void GetValues(ref ArgusLight.Fractals2D.ColoringParameter cParameter)
		{
			PeriodicColoringParameter para = (PeriodicColoringParameter)cParameter;
			para.AutoSetFrequency = (bool)this.cbAutoSetFrequency.IsChecked;
			para.Translation = this.cTranslation.Value;
			para.Frequency = this.cFrequency.Value;
			para.NumberOfColorPeriods = this.cNumberOfColorPeriods.Value;
			cParameter = para;
		}

		private void UIColoringParameter_IsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
		{
			if ((bool)e.NewValue == false)
				this.timer.Stop();
			else if (this.cbAnimate.IsChecked == true)
				this.timer.Start();
		}
	}
}
